package com.telaxo.shooter2012 {
	import com.telaxo.shooter2012.gfx.Nuage;
	import com.telaxo.shooter2012.gfx.Herbe;
	import com.telaxo.shooter2012.gfx.Ciel;
	import tlx.debug.movieMonitor;

	import com.telaxo.shooter2012.foes.Stinger;

	import caurina.transitions.Tweener;

	import com.telaxo.shooter2012.bonus.Bonus;
	import com.telaxo.shooter2012.factory.EnemyFactory;
	import com.telaxo.shooter2012.gfx.ScoreHUD;
	import com.telaxo.shooter2012.gfx.Ship;
	import com.telaxo.shooter2012.gfx.Star;

	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.ui.Mouse;

	public class Engine extends MovieClip {
		private static var myPause : Boolean;
		public static var myInstance : Engine = null;
		private var myCurrentState : Sprite;
		private var myBonus : Bonus;
		private var fond : Fond;
		private var ourShip : Ship;
		public static var listeEnnemis : Vector.<MovieClip>;
		private var scoreHUD : ScoreHUD;
		private var herbe : Herbe;
		private var stageRef : Stage;
		private var ciel : Ciel;

		public function Engine(stageRef : Stage) {
			this.stageRef = stageRef;
			//Debugger
			// var mov:movieMonitor = new movieMonitor();
			// mov.x = stageRef.stageWidth;
			// stageRef.addChildAt(mov, 0);
		
			addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
		}

		private function init(event : Event) : void {
			Mouse.hide();
			herbe = new Herbe(stage);
			ourShip = new Ship(stage, this);
			listeEnnemis = new Vector.<MovieClip>();
			ciel = new Ciel(stage);
			// Ajout des Bonus
			myBonus = new Bonus(ourShip);
			ciel.x = 0;
			ciel.y = 0;
			// Place l'orgine au centre des objets
			// setRegPoint(ourShip, 25, 25);
			//setRegPoint(fondfond, 250, 200);

			stage.addChild(ciel);
			stage.addChild(new Nuage());
			stage.addChild(herbe);
			stage.addChild(ourShip);
			stage.addChild(myBonus);
			// Création de l'HUD
			scoreHUD = new ScoreHUD(stage);
			stage.addChild(scoreHUD);
			
			// Positionne le vaisseau au milieu
			ourShip.x = stage.stageWidth / 2;
			ourShip.y = stage.stageHeight / 2;
			stage.frameRate = 60;

			addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
		}

		public static function isPaused() : Boolean {
			return myPause;
		}

		// Permets de déplacer l'axe des origines des objets
		private function setRegPoint(obj : DisplayObjectContainer, newX : Number, newY : Number) : void {
			var bounds : Rectangle = obj.getBounds(obj.parent);
			var currentRegX : Number = obj.x - bounds.left;
			var currentRegY : Number = obj.y - bounds.top;

			var xOffset : Number = newX - currentRegX;
			var yOffset : Number = newY - currentRegY;

			obj.x += xOffset;
			obj.y += yOffset;

			for (var i : int = 0; i < obj.numChildren; i++) {
				obj.getChildAt(i).x -= xOffset;
				obj.getChildAt(i).y -= yOffset;
			}
		}

		// boucle principale
		private function loop(pEvt : Event) : void {
			var rand : uint = Math.random() * 90;
			if (Math.floor(rand) == 5) {
				// Create an enemy
				//var enemy : MovieClip = EnemyFactory.createEntity("STINGER", stageRef, ourShip);
				// Add an event on the enemy
				
				//enemy.addEventListener(Event.REMOVED_FROM_STAGE, supprimerEnnemi, false, 0, true);
				// Add an enemy
				var enemy:Stinger = new Stinger(stageRef, ourShip);
				stageRef.addChild(enemy);
				enemy.addEventListener(Event.REMOVED_FROM_STAGE, supprimerEnnemi, false, 0, true);
				listeEnnemis.push(enemy);
			}
		}

		private function supprimerEnnemi(e : Event) : void {
			listeEnnemis.splice(listeEnnemis.indexOf(e.currentTarget), 1);
		}

		public function switchState(state : Sprite) : void {
			trace(this + "switchState(" + state);
			removeChild(myCurrentState);
			myCurrentState = state;
			addChild(myCurrentState);
			stage.focus = stage;
		}

		public function load() : void {
			stageRef.addChild(this);
			Tweener.addTween(this, {alpha:1, y:0, time:0.7});
		}

		public static function togglePause(param : Boolean = true) : void {
			myPause = param;
		}
		
		// ACCESSEURS
		//Récupére l'interface du jeu
		public function get scoreHud() : ScoreHUD {
			return scoreHUD;
		}
	}
}